package ws.utils.collections.table;

/**
 * HTML representation of the table.
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class TableHTML extends Table {

  /**
   * Create new instance.
   */
  public TableHTML() {
  }

  /**
   * Create new instance from other table.
   *
   * @param table table to convert.
   */
  public TableHTML(ITable table) {
    setHeader(table.getHeader());
    for(Row row : table.getRowList()) {
      addRow(new RowHTML(row));
    }
  }

  /**
   * Create a string that has HTML code that represents the data in this table.
   *
   * @return HTML code.
   */
  @Override
  public String toString() {
    String html = "<table border=\"1\">\n";
    /* Print header. */
    for(Column col : header.getColumnList()) {
      html += "  <th>";
      html += col.content;
      html += "</th>\n";
    }
    /* Print rows. */
    for(Row row : rowList) {
      html += "  ";
      html += row.toString();
    }
    html += "</table>\n";
    return html;
  }

  /**
   * Convert table to HTML representation.
   *
   * @param table table to be converted.
   *
   * @return representation of {@code table} in HTML code.
   */
  public static String toHTML(ITable table) {
    TableHTML thtml = new TableHTML(table);

    return thtml.toString();
  }
}
